package com.lib.consle;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;

import com.lib.DB.DBHelper;
import com.lib.Serialize.objectSerializeUtil;
import com.lib.TrapDoor.GenTrapDoor;

import com.lib.index.buildIndex;
import com.lib.index.indexEncrypted;
import com.lib.index.indexHelper;
import com.lib.model.encryptedIndex;
import com.lib.model.fileScorePair;
import com.lib.search.searchModule;

public class MainConsle {
	public static void main(String []args)
	{
		boolean flag=true;
		String strChose="";
		int chose=-1;
		Pattern pattern = Pattern.compile("[0-9]*");
		Map<String, encryptedIndex>encryptedIndexInfo=null;//定义全局的加密索引
	//	List<String>wordList=DBHelper.getAllDistinctWord();
		while(flag)
		{
			System.out.println("--------------------MRSE_TF方案性能测试--------------------");
			System.out.println("1.文件上传（生成加密索引相关功能）");
			System.out.println("2.多关键字搜索");
			System.out.println("3.退出");
			Scanner scanner=new Scanner(System.in);
			strChose=scanner.next();
			if(pattern.matcher(strChose).matches())
			{
				chose=new Integer(strChose);
				switch(chose)
				{
				case 1:
				{
					System.out.println("······开始构建加密索引······");
					long startTime=System.currentTimeMillis();
					List<String> fileNameList=DBHelper.getAllFileName();
					indexEncrypted inSK=buildIndex.getEncryptedSK();
					encryptedIndexInfo=buildIndex.getAllEncryptedIndexs(fileNameList,inSK);
					inSK=null;//垃圾回收
					long endTime=System.currentTimeMillis();
					System.out.println("构建加密索引时间是："+(endTime-startTime)+"ms");
					try {
						objectSerializeUtil.serialize(encryptedIndexInfo,"encryptedIndexInfo.obj");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//encryptedIndexInfo=null;
					fileNameList=null;//垃圾回收
					System.out.println("······加密索引构建结束······");
					break;
				}
				case 2:
				{
					System.out.println("请输入你要搜索的关键字(输入exit表示结束)：");
					Scanner scWordInput=new Scanner(System.in);
					List<String> searchWordList=new ArrayList<String>();
					String line = "";
			        while (scWordInput.hasNextLine()) {
			            line = scWordInput.nextLine();
			            if ("exit".equals(line)) {
			                break;
			            }
			            if(DBHelper.isWordExists(line))
			            {
			            	searchWordList.add(line);
			            }
			            else {
							System.out.println("输入关键字无效，请重新输入：");
						}
			        }
			        System.out.println("请输入想获取文件的个数：");
				      int mostK=scWordInput.nextInt();
					encryptedIndex trapDoor=GenTrapDoor.getEncryptedSearchIndex(searchWordList);
					if(encryptedIndexInfo==null)
					{
						try {
							encryptedIndexInfo=(Map<String, encryptedIndex>) objectSerializeUtil.deserialize("encryptedIndexInfo.obj");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					System.out.println("······多关键字搜索开始······");
					long startTime=System.currentTimeMillis();
					List<fileScorePair>RelvlentFileInfo=searchModule.getRelvlentFileInfo(encryptedIndexInfo, trapDoor);
					System.out.println("测试RelvlentFileInfo:");
					for(int i=0;i<RelvlentFileInfo.size();i++)
					{
						System.out.println(RelvlentFileInfo.get(i).getFileName()+":"+RelvlentFileInfo.get(i).getScore());
					}
					List<String> mostKFileList=searchModule.getMostRelateFile(RelvlentFileInfo, mostK);//得到最相关的1个文件
					System.out.println("最相关的文件是：");
					System.out.println(mostKFileList);
					long endTime=System.currentTimeMillis();
					System.out.println("搜索过程用的时间是："+(endTime-startTime)+"ms");
					System.out.println("······多关键字搜索结束······");
					break;
				}
				case 3:
				{
					flag=false;
					break;
				}
				default:
				{
					break;
				}
				}
			}
			else {
				System.out.println("输入的不是整数，请重新输入！");
			}
			
		}
		
		
	}

}